//  SuccessViewController.m
//  LoginDemo
//
//  Created by XFB on 2017/6/12.
//  Copyright © 2017年 ZC. All rights reserved.
//

#import "SuccessViewController.h"
#import "CusutomNaviBar.h"

#import "OneViewController.h"
#import "TwoViewController.h"

static const CGFloat ratio = 0.8f;
static const CGFloat headHeight = 160.0f;

@interface SuccessViewController () <UITableViewDelegate, UITableViewDataSource, CusutomNaviBarDelegate>
{
    CGRect originalFrame;
}
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) CusutomNaviBar *customNaviBar;

@end

@implementation SuccessViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.fd_prefersNavigationBarHidden = YES;
}

- (void)setupUI
{
    //
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ratio * ScreenWidth)];
    originalFrame = self.bgImageView.frame;
    self.bgImageView.image = [UIImage imageNamed:@"bgImage"];
    [self.view addSubview:self.bgImageView];
    
    self.customNaviBar = [[CusutomNaviBar alloc] init];
    self.customNaviBar.delegate = self;
    self.customNaviBar.title = @"测试Demo";
    self.customNaviBar.titleColor = [UIColor whiteColor];
    self.customNaviBar.leftImage = @"header_leftbtn_nor_original";
    self.customNaviBar.rightImage = @"header_icon_add_original";
    [self.view addSubview:self.customNaviBar];

    // 创建tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, ScreenWidth, headHeight)];
    tableView.tableHeaderView = headView;
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

#pragma mark --- <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset = scrollView.contentOffset.y;

    if (yOffset < headHeight) { //上推，tableView的偏移量还没有到导航栏的底部
        
        CGFloat alpha = yOffset/headHeight;
        self.customNaviBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
        self.customNaviBar.leftImage = @"header_leftbtn_nor_original";
        self.customNaviBar.rightImage = @"header_icon_add_original";
        self.customNaviBar.titleColor = [UIColor whiteColor];

    } else if (yOffset > headHeight) { //同样上推，tableView的偏移量已经过了导航栏底部
        
        self.customNaviBar.backgroundColor = [UIColor whiteColor];
        self.customNaviBar.leftImage = @"header_leftbtn_press_original.png";
        self.customNaviBar.rightImage = @"header_icon_add";
        self.customNaviBar.titleColor = [UIColor blackColor];
    }

    if (yOffset > 0) { //上推
    
        CGRect frame = originalFrame;
        frame.origin.y -= yOffset;
        self.bgImageView.frame = frame;

    } else { //下拉
       
        CGRect frame = originalFrame;
        frame.size.width += (-yOffset);
        frame.size.height = frame.size.width * (originalFrame.size.height / originalFrame.size.width);
        frame.origin.x -= (frame.size.width - originalFrame.size.width)/2;
        self.bgImageView.frame = frame;
        
    }
}


#pragma mark --- <CusutomNaviBarDelegate>

- (void)customNaviBarLeftButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)customNaviBarRightButtonClick
{
    // 没有导航栏 -> 跳转到导航栏
//    OneViewController *oneVc = [[OneViewController alloc] init];
//    [self.navigationController pushViewController:oneVc animated:YES];
    
    // 没有导航栏 -> 跳转到没有导航栏
    TwoViewController *vc = [[TwoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end



